package com.asiainfo.util.jackjson;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * java日期对象经过Jackson库转换成JSON日期格式化自定义类
 * 
 * <pre>
 * &#064;JsonSerialize(using=CustomDateTimeSerializer.class)
 * public Date getTime(){
 * 	return time;
 * }
 * </pre>
 * 
 * @author luhf
 * @date 2013-1-13 下午8:08:05
 */
public class CustomDateTimeSerializer extends JsonSerializer<Date>{

	private static final Logger log=LoggerFactory.getLogger(CustomDateTimeSerializer.class);

	static{
		log.debug("启动CustomDateTimeSerializer工具类!");
	}

	/**
	 * 私用构造主法.因为此类是工具类.
	 */
	public CustomDateTimeSerializer(){
		super();
	}

	@Override
	public void serialize(Date value,JsonGenerator jgen,SerializerProvider provider) throws IOException, JsonProcessingException{
		jgen.writeString(DateFormatUtils.format(value,"yyyy-MM-dd HH:mm:ss"));
	}
}